<template>
  <div class="login_container">
    <el-row justify="center">
      <el-col :span="8" :xs="0"></el-col>
      <el-col :span="8" :xs="22">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>hello</h1>
          <h2>欢迎来到后台管理系统</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/times'
//加载按钮
let loading = ref(false)
let userStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
let loginForm = reactive({ username: 'admin', password: '111111' })
let loginForms = ref()
const login = async () => {
  await loginForms.value.validate()

  //开始加载
  loading.value = true
  //sucess
  //fail

  try {
    await userStore.userLogin(loginForm)
    //编程式导航
    let redirect: any = $route.query.redirect

    $router.push({ path: redirect || '/' })
    ElNotification({
      type: 'success',
      message: '欢迎回来！',
      title: 'HI,' + getTime() + '好',
    })
    loading.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}

const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5 && value.length <= 15) {
    callback()
  } else {
    callback(new Error('账号长度5-15位'))
  }
}

const strongPasswordRegex = new RegExp(
  '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\\$%\\^&\\*])(.{8,})$',
)
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (
    value.length >= 5 &&
    value.length <= 15 /*  &&
    strongPasswordRegex.test(value) */
  ) {
    callback()
  } else {
    callback(new Error('密码长度5-15位'))
  }
}
const rules = {
  username: [
    {
      trigger: 'change',
      validator: validatorUserName,
    },
  ],
  password: [
    {
      trigger: 'change',
      validator: validatorPassword,
    },
  ],
  /*   username: [
    {
      required: true,
      min: 5,
      max: 10,
      message: '长度应该为5-10位',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: '长度应该为6-15位',
      trigger: 'change',
    },
  ], */
}
</script>
<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 20vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 2.8rem;

    h1 {
      color: white;
      font-size: 2.8rem;
    }

    h2 {
      color: white;
      font-size: 1.5rem;
      margin: 1rem 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
